home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / shell-tools / pg / pg.c < prev    next >
C/C++ Source or Header  |  1996-03-05  |  2KB  |  108 lines

  1. /* pg
  2.    Source for Manx Aztec C
  3.    Freeware, (C) Thomas Radtke 1996
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <sgtty.h>
  8.  
  9. /* we try to determine the 'visible' stringsize */
  10.  
  11. int strlen_(char *ptr)
  12. {
  13.     int i=0,z=0;
  14.     unsigned char c;
  15.  
  16.     while ((c=ptr[i++])!='\0') {
  17.         if (c<32) {
  18.             switch(c) {
  19.                 case '\t': /* tab */
  20.                     z+=7;
  21.                     break;
  22.                 case 8: /* backspace */
  23.                     z-=2;
  24.                     break;
  25.                 default: /* unknown, could be terribly wrong :( */
  26.                     z-=1;
  27.                     break;
  28.             }
  29.         }
  30.     }
  31.     return i+z-1;
  32. }
  33.  
  34. void rawmode(struct FileHandle *fh)
  35. {
  36.     struct sgttyb stty;
  37.  
  38.     ioctl(fh,TIOCGETP,&stty);
  39.     stty.sg_flags|=RAW;
  40.     ioctl(fh,TIOCSETP,&stty);
  41. }
  42.  
  43. void conmode(struct FileHandle *fh)
  44. {
  45.     struct sgttyb stty;
  46.  
  47.     ioctl(fh,TIOCGETP,&stty);
  48.     stty.sg_flags&=~RAW;
  49.     ioctl(fh,TIOCSETP,&stty);
  50. }
  51.  
  52. int forw(int old_pos,char *buffer)
  53. {
  54.     while (buffer[old_pos++]!=';');
  55.     return old_pos;
  56. }
  57.  
  58. main(int argc, char **argv)
  59. {
  60.     FILE *fp_read,*fp_write,*std_in;
  61.     char *buffer;
  62.     char c,x[5],y[5];
  63.     int i,idx,pos,row,col,len;
  64.  
  65.     if (!(buffer=(char *)malloc(1024))) exit(20);
  66.     fp_read=fopen("*","r");
  67.     fp_write=fopen("*","w");
  68.     len=strlen(argv[argc-1]);
  69.     if (!strcmp(&argv[argc-1][len-2],"pg") || !(std_in=fopen(argv[argc-1],"r"))) std_in=stdin;
  70.     rawmode(fileno(fp_write));
  71.     fprintf(fp_write,"%c0 q",0x9b);
  72.     fscanf(fp_read,"%[^r]",buffer);
  73.     getc(fp_read);
  74.     conmode(fileno(fp_write));
  75.     pos=forw(1,buffer);
  76.     pos=forw(pos,buffer);
  77.     sscanf(&buffer[pos],"%[^;]",y);
  78.     pos=forw(pos,buffer);
  79.     sscanf(&buffer[pos],"%[^;]",x);
  80.     row=atoi(y);
  81.     col=atoi(x);
  82.     i=0;
  83.     idx=0;
  84.     for(;;) {
  85.         while (i<row-1) {
  86.             if (!idx) {
  87.                 if (!fgets(buffer,1024,std_in)) exit(0);
  88.                 i+=(1+strlen_(buffer)/col);
  89.             }
  90.             else idx=0;
  91.             if (i>=row-1) idx=1;
  92.             else printf("%s",buffer);
  93.         }
  94.         i=0;
  95.         printf("[More]");
  96.         fflush(fp_write);
  97.         rawmode(fileno(fp_write));
  98.         if (getc(fp_read)=='q') {
  99.             printf("%c6D      %c6D",0x9b,0x9b);
  100.             conmode(fileno(fp_write));
  101.             while (fgets(buffer,1024,std_in)) printf("%s",buffer);
  102.             exit(0);
  103.         }
  104.         printf("%c6D      %c6D",0x9b,0x9b);
  105.         conmode(fileno(fp_write));
  106.     }
  107. }
  108.